import Vue from "vue";
import VueRouter from "vue-router";

import Login from "../views/login/Login";
import Home from "../views/Home";
import User from "../views/system/User";
import Role from "../views/system/Role";
import Index from '../views/Index'
import Info from '../views/business/medicine/info'
import Overdue from '../views/business/medicine/overdue'
import Patient from '../views/business/patient'
import Hello from "../components/HelloWorld"

Vue.use(VueRouter)

const routes = [
        {
            path: "/hello",
            name: "Hello",
            meta: {title: "hello"},
            component: Hello
        },
        {
            path: '/login',
            name: 'Login',
            component: Login
        },
        {
            path: "/",
            name: "Home",
            component: Home,
            children: [
                {
                    path: '/home',
                    name: 'Index',
                    meta: {title: "首页"},
                    component: Index
                },

                {
                    path: "/user",
                    name: "User",
                    meta: {title: "用户管理", parentTitle: "系统管理"},
                    component: User
                },
                {
                    path: "/role",
                    name: "Role",
                    meta: {title: "角色管理", parentTitle: "系统管理"},
                    component: Role
                },
                {
                    path: '/medicine/info',
                    name: 'Info',
                    meta: {title: "药品信息"},
                    component: Info
                },
                {
                    path: '/medicine/overdue',
                    name: 'Overdue',
                    meta: {title: "过期药品"},
                    component: Overdue
                },
                {
                    path: '/patient',
                    name: 'Patient',
                    meta: {title: "病人管理"},
                    component: Patient
                }

            ]
        },
        // 404界面（必须放置在路由最底部）
        {
            path: "*",
            component: () =>
                import("@/views/notFound.vue")
        }

    ]
;

const router = new VueRouter({
    mode: "history",
    base: process.env.BASE_URL,
    routes
});

// 页面跳转回到最顶部
router.afterEach((to, from, next) => {
    window.scrollTo(0, 0)
})

router.beforeEach((to, from, next) => {
    //console.log(to)
    //console.log(to.path)
    if (to.path != '/login') {
        const token = window.sessionStorage.getItem("token");
        // 未登录拦截
        if (!token) {
            next({path: '/login'})
            return
        }
    }
    next()
})

const routerPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
    return routerPush.call(this, location).catch(error => error)
}

export default router;
